package org.fuxian.web.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.fuxian.common.Constraints;
import org.fuxian.domain.MyArea;
import org.fuxian.service.IMyAreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/area")
public class MyAreaController {

    @Autowired
    IMyAreaService areaService;


    @RequestMapping("/findAllAreaPageData")
    @ResponseBody
    public Object findAllAreaPageData(int current) {
        PageHelper.startPage(current, Constraints.PAGE_SIZE);
        List<MyArea> list = areaService.findAll();
        return new PageInfo(list);
    }

    @RequestMapping("/findAllArea")
    @ResponseBody
    public Object findAllArea() {
        List<MyArea> list = areaService.findAll();
        return list;
    }


    @RequestMapping("/getArea")
    @ResponseBody
    public String getArea(String sname) {
        MyArea myArea = areaService.findAreaByName(sname);
        int count = 0;
        if (myArea != null)
            count = 1;
        return count + "";
    }

    @RequestMapping("/addArea")
    @ResponseBody
    public String addArea(String aname) {
        int count = areaService.add(aname);
        return count + "";
    }

    @RequestMapping("/toUpdate/{aid}")
    public String toUpdate(@PathVariable("aid") Integer aid, Model model) {
        MyArea myArea = areaService.findById(aid);
        model.addAttribute("area", myArea);
        return "area/upp";
    }

    @RequestMapping("/update")
    @ResponseBody
    public String update(Integer aid, String aname, Model model) {
        int count = areaService.update(aname,aid);
        return count + "";
    }

    @RequestMapping("/del")
    @ResponseBody
    public String del(Integer aid) {
        int count = areaService.delete(aid);
        return count + "";
    }
}
